/*
 * Tidal - an IDE for Google Wave gadgets.
 * Copyright (C) 2010 Nathan Summers
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.tidal.ide;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.tidal.ide.file.File;
import com.googlecode.tidal.ide.file.FileStore;


public class FileServlet extends HttpServlet {
    static Logger log = Logger.getLogger(FileServlet.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException {
	FileStore fileStore = Config.getFileStore();
	
	log.finest("Path:" + req.getPathInfo());
	
	File file = fileStore.retrieve(req.getPathInfo());
	
	if (file == null) {
	    resp.sendError(404, "Cannot find the file you requested.  It might have expired from the Tidal cache.");
	    return;
	}
	
	resp.setContentType(file.getMimeType());
	resp.getWriter().write(file.getContents());
    }
}